package com.ebelter.bodyfatscale.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.ebelter.bodyfatscale.R;
import com.ebelter.bodyfatscale.common.AppDateLitConstants;
import com.ebelter.bodyfatscale.common.Constants;
import com.ebelter.bodyfatscale.moudules.db.Daos;
import com.ebelter.bodyfatscale.moudules.db.beans.UsersData;
import com.ebelter.bodyfatscale.util.MyTextWatcher;
import com.ebelter.bodyfatscale.util.NumUtils;
import com.ebelter.bodyfatscale.util.ToastUtil;
import com.ebelter.bodyfatscale.util.UserSpUtil;
import com.ebelter.btlibrary.btble.impl.scale.model.ScaleUser;

import butterknife.BindView;
import butterknife.OnClick;

//修改身高界面
public class ModifyHeightActivity extends BaseActivity {


    public static String ORIGINAL_HEIGHT = "ORIGINAL_HEIGHT";
    public static final int REQUESTCODE = 107;


    @BindView(R.id.modifyheight_back_bt)
    ImageView modifyheight_back_bt;

    @BindView(R.id.et_height_pager)
    EditText et_height_pager;

    @BindView(R.id.modifyheight_done_tv)
    TextView modifyheight_done_tv;

    @Override
    protected int loadLayoutById() {
        return R.layout.activity_modifyheight;
    }

    @Override
    protected void initViews() {

    }

    int originalHeight;
    @Override
    protected void initData() {

        originalHeight = getIntent().getIntExtra(ORIGINAL_HEIGHT,-1);
        if (originalHeight==-1){
            finish();
        }
        et_height_pager.setText(originalHeight+"");
        et_height_pager.addTextChangedListener(new MyTextWatcher() {
            @Override
            public void afterTextChanged(Editable s) {
                String height=s.toString();
                if (TextUtils.isEmpty(height)){
                    modifyheight_done_tv.setEnabled(false);
                }else {
                    modifyheight_done_tv.setEnabled(true);
                }
//                int h = NumUtils.string2Int(height);
//                if (h< AppDateLitConstants.HEIGHT_MIN||h>AppDateLitConstants.HEIGHT_MAX){
//                    ToastUtil.show(R.string.Please_enter_a_value_between_20_and_250_height);
//                    ViewUtils.shakeView(et_height_pager);
//                    modifyheight_done_tv.setEnabled(false);
//                }else {
//                    modifyheight_done_tv.setEnabled(true);
//                }
            }
        });

    }


    @OnClick({ R.id.modifyheight_back_bt,R.id.modifyheight_done_tv})
    public void btClick(View view){
        switch (view.getId()){
            case R.id.modifyheight_back_bt:
                finish();
                break;
            case R.id.modifyheight_done_tv:
               // ToastUtil.show(modify_ed.getText().toString());
                done();
                break;


        }
    }

    private void done() {

        String height=et_height_pager.getText().toString().trim();
        int h = NumUtils.string2Int(height);
        if (h< AppDateLitConstants.HEIGHT_MIN||h>AppDateLitConstants.HEIGHT_MAX){
            ToastUtil.show(R.string.Please_enter_a_value_between_20_and_250_height);
            return;
        }
        if (h==originalHeight){
            ToastUtil.show(R.string.Modify_Success);
            finish();
            return;
        }

        long userid = UserSpUtil.readLong(Constants.IUser.USER_ID);
        UsersData usersData = Daos.getInstance().getUsersDataFromUserId(userid);
        usersData.height=h;
        usersData.update();
        ToastUtil.show(R.string.Set_success);

        UserSpUtil.writeInt(Constants.IUser.HEIGHT, h);
        ScaleUser.getUser().setHeight((int) h);
        originalHeight=h;


    }


}
